window.VideoSearch = Class.create();
Object.extend(VideoSearch.prototype, BaseModule.prototype);
Object.extend(VideoSearch.prototype, {
//woc 제출

	title : "비디오검색",
	configurable : false,
	maxmizable : false,

	setContentDiv : function()
	{
	},


	onLoad : function(tr)
	{	
			

//함수들

var selectParser= function(site,readXML){

	switch(site){
    case 'youtube' : { youtubeParser(readXML); break;}
    case 'google' : { googleParser(readXML); break; }
    case 'tagstory' : { tagstoryParser(readXML); break; } 
	case 'mncast' : { mncastParser(readXML); break; } };

};

var turnPage = function( createList , total_len , current_len) {
    
	
	var turnSpan = document.createElement('span');
	pageDiv.appendChild(turnSpan);

	if(current_len > 0) {
		var prevPage = document.createElement('a');
        prevPage.href = '#';
        prevPage.onclick = function(e) {
            current_len -= pagePerItem; 
			cleanUp(listDiv); cleanUp(pageDiv);
			createList(current_len); 
            return false; }.bindAsEventListener(this);
        prevPage.innerHTML += '<img class="funcbut lbut" src="/images/w-btn_prev2.gif" />';
        turnSpan.appendChild(prevPage);
	}	
	
	if(current_len+pagePerItem < total_len )  { 
        var nextPage = document.createElement('a');
        nextPage.href='#';
        nextPage.onclick = function(e){
            current_len += pagePerItem; 
			cleanUp(listDiv); cleanUp(pageDiv);  
			createList(current_len); 
            return false; }.bindAsEventListener(this);
        nextPage.innerHTML  += '<img class="funcbut rbut" src="/images/w-btn_next2.gif" />';
        turnSpan.appendChild(nextPage);
        //if( item_len > videos.length  ){ pageDiv.removeChild(nextPage); }
	}    
	
 };


var playerEvent = function(e) {
	var e = Event.element(e);
	if(e.nodeName =='IMG') { e = e.parentNode; }
 	
	var size = setPlayerSize(t.offsetWidth);
	playerDiv.innerHTML = '<embed src='+e.getAttribute('href')+' type="application/x-shockwave-flash" width='+size[0]+' height='+size[1]+'>';
    playerDiv.innerHTML += '<br><div class=tit2 >'+e.getAttribute('title')+'</div>';
	playerDiv.innerHTML += '<br><a href='+e.getAttribute('href')+' target="_blank">전체화면'+'</a>';
	playerDiv.innerHTML += '<a href=# >화면숨기기</a>';
	playerDiv.getElementsByTagName('a')[1].onclick = function(e) {	cleanUp(playerDiv); return false; }.bindAsEventListener(this);

};


var setPlayerSize = function(contentWidth) {
	
	var width = 420;
	var height = 380;
	
	var playerWidth = parseInt(contentWidth)-15;
	if(playerWidth <width) {
		var ratio = playerWidth /width;
		width = playerWidth;
		height *= ratio;
	}

	return [width,height]; 

};


var cleanUp= function(node) {

	var length = node.childNodes.length;
	for(var i = 0 ; i<length ; i++) {	
		node.removeChild(node.firstChild);
	}
//	node.innerHTML ='';
	
};


var selectTab = function(e){
	siteUl.childNodes[tab_flag].className='li';
    e.parentNode.className ="li on";
	tab_flag = siteList.indexOf(site);
};






//화면 정리
	cleanUp(this.contentDiv);



//구성-테두리 
    var t= this.contentDiv;
    var playerDiv = document.createElement('div');
    var searchDiv = document.createElement('div');
    var tabDiv = document.createElement('div');
    var listDiv = document.createElement('div');
    var pageDiv = document.createElement('div');
	
	//검색창, 탭, 플레이어, 검색결과 리스트, 다음-이전버튼 순 
	t.appendChild(searchDiv);	
	t.appendChild(tabDiv);		
	t.appendChild(playerDiv);
	t.appendChild(listDiv);	
	t.appendChild(pageDiv);
	
	pageDiv.className ="prevnext";
	playerDiv.className="movie";



//변수 	
	var keyword =null; 
	var site = 'google';
	var pagePerItem = 4;	
	var tab_flag= 0;



//탭-사이트 선택

	var siteList = [ 'google','tagstory','youtube' ];
 	var siteUl = document.createElement('ul');
	tabDiv.appendChild(siteUl);
	siteUl.className ="tabs";
	
	siteList.each( function( siteName ){
		var siteLi = document.createElement('li');
		siteUl.appendChild(siteLi);
		siteUl.childNodes[tab_flag].className='li on';
		
		siteLi.innerHTML += '<a href=# value='+siteName+'>'+siteName+'</a>';
		siteLi.firstChild.onclick=  function(e) {
            var e = Event.element(e);
			site = e.getAttribute('value');
            if(!keyword) { selectTab(e); return false; }
            this.request( { keyword : encodeURIComponent(keyword) , site: site  },
                function(tr){
                selectTab(e);
                cleanUp(listDiv); cleanUp(pageDiv);
				var readXML = tr.responseXML;
               	selectParser(site,readXML);
                }
            ); //request end
            return false;
        }.bindAsEventListener(this);
 		
	}.bind(this));
	 
	


//요청-폼	

    var form = document.createElement('form');
	form.className='panel';
    form.method = 'post';
	form.innerHTML += '<input class=query type = text name = keyword />';
	form.innerHTML += '<input type = hidden />';
	searchDiv.appendChild(form);

    var search = document.createElement('input');
    search.type ='image';
// search.value='검색';
	search.className ='find';
	search.src = '/images/w-btn_find.gif';
    form.appendChild(search);

    form.onsubmit = function(e){
		var e= Event.element(e);
		keyword = e.childNodes[0].value;
		
		if(!keyword) { return false; }
		this.request( { keyword : encodeURI(keyword), site: site }, 
			function(tr){ 
			cleanUp(listDiv); cleanUp(pageDiv); 
			var readXML = tr.responseXML;
			selectParser(site,readXML);		
			}
		); //request end		
		return false;
	}.bindAsEventListener(this);




// 유투브 

var youtubeParser= function(readXML){	

	var ut_response = readXML.getElementsByTagName('ut_response')[0];
	var video_list = ut_response.getElementsByTagName('video_list')[0];
    var videos = video_list.getElementsByTagName('video');
	if( videos.length ==0 ) { listDiv.innerHTML ='검색결과가 없습니다.'; return false; }

	var item_len = 0;
	
	var youtubeList= function(item_len){ 
		for(var i = item_len ; i< item_len+pagePerItem && i < videos.length; i++) {
			
			var itemDiv= document.createElement('div');
			listDiv.appendChild(itemDiv);
		    var descDiv = document.createElement('div');
			var aDiv = document.createElement('div');
            var descDiv2 = document.createElement('div');

            itemDiv.appendChild(aDiv);
            itemDiv.appendChild(descDiv);
            itemDiv.appendChild(descDiv2);

            itemDiv.className = "item";
            aDiv.className = "thumb";
            descDiv.className = "desc tit mar100";
            descDiv2.className = "desc tag mar100";

			
			var video = videos.item(i);
			var player_url = 'http://www.youtube.com/v/'+ video.getElementsByTagName('id')[0].firstChild.nodeValue;
			var title = video.getElementsByTagName('title')[0].firstChild.nodeValue;
			
			aDiv.innerHTML += '<a href='+player_url+' title="'+title+'">'+
				'<img class="tn" src="'+ video.getElementsByTagName('thumbnail_url')[0].firstChild.nodeValue+'" ></a>';
			aDiv.firstChild.onclick= function(e) { playerEvent(e); return false; }.bindAsEventListener(this);		

			
			descDiv.innerHTML += '<a href='+player_url+' title="'+title+'">'+
				video.getElementsByTagName('title')[0].firstChild.nodeValue+'</a>';
			descDiv.firstChild.onclick= function(e){ playerEvent(e); return false; }.bindAsEventListener(this);
			
			var tags = video.getElementsByTagName('tags')[0].firstChild;
			
			descDiv2.innerHTML += '<a href='+player_url+'>'+video.getElementsByTagName('title')[0].firstChild.nodeValue + 
				'</a><br>'+ video.getElementsByTagName('author')[0].firstChild.nodeValue+'<br>';
			if(tags) descDiv2.innerHMTL += 'Tag: ' + tags.nodeValue;
			
			
		}//for	 
		
		var allsee = document.createElement('span');
    	pageDiv.appendChild(allsee);
		allseeUrl = ut_response.getElementsByTagName('allsee')[0].getAttribute('url'); 
    	allsee.innerHTML += '<a href="'+allseeUrl+'"><img class=lbut src=images/w-btn_viewall.gif></a>'; 
				
        turnPage(youtubeList, videos.length, item_len);

	}; //youtubeList	
	
	youtubeList(item_len);

}; //youtubeParser


//태그스토리

var tagstoryParser = function(readXML){ 
		
	var rss = readXML.getElementsByTagName('rss')[0];
	var channel = rss.getElementsByTagName('channel')[0];
	var items = channel.getElementsByTagName('item');
    if( items.length ==0 ) { listDiv.innerHTML ='검색결과가 없습니다.'; return false; }
	
	var item_len = 0;

	var tagstoryList = function(item_len){
	
		for (var j= item_len; j < item_len+pagePerItem && j< items.length; j++) {
			
			var itemDiv= document.createElement('div');
		    listDiv.appendChild(itemDiv);
		    var descDiv = document.createElement('div');
		    var aDiv = document.createElement('div');
            var descDiv2 = document.createElement('div');

            itemDiv.appendChild(aDiv);
            itemDiv.appendChild(descDiv);
            itemDiv.appendChild(descDiv2);

            itemDiv.className = "item";
            aDiv.className = "thumb";
            descDiv.className = "desc tit mar100";
            descDiv2.className = "desc tag mar100";


			var item = items.item(j);
			var item_url = document.createTextNode(item.getElementsByTagName('link')[0].firstChild.nodeValue);
			var player_url= 'http://play.tagstory.com/player/TS00@' + item_url.substringData(item_url.length-10 ,10);
			var title = item.getElementsByTagName('title')[0].firstChild.nodeValue;

			var browser = navigator.appName;
			if( browser == 'Microsoft Internet Explorer') var thumbnail = 'media:thumbnail';
            else var thumbnail = 'thumbnail';

			aDiv.innerHTML +='<a href='+player_url+' title="'+title+'">'+
				'<img class=tn src='+item.getElementsByTagName(thumbnail)[0].getAttribute('url')+'></a>';
			aDiv.firstChild.onclick =function(e) { playerEvent(e); return false; }.bindAsEventListener(this);
			
			descDiv.innerHTML += '<a href='+player_url+' title="'+title+'">'+title+'</a><br>';
			descDiv.firstChild.onclick = function(e) { playerEvent(e); return false; }.bindAsEventListener(this);

			descDiv2.innerHTML += item.getElementsByTagName('author')[0].firstChild.nodeValue+'<br>';
			descDiv2.innerHTML += 'Tag'+item.getElementsByTagName('category')[0].firstChild.nodeValue+'<br>';
		
		} //for

		
		var allsee = document.createElement('span');
        pageDiv.appendChild(allsee);
        allseeUrl = rss.getElementsByTagName('allsee')[0].getAttribute('url');			
		allsee.innerHTML += '<a href='+allseeUrl+'><img class=lbut src=images/w-btn_viewall.gif></a>';

		turnPage(tagstoryList, items.length, item_len);
	};
	
	tagstoryList(item_len);

};// tagstoryParser	




//구글비디오

var googleParser = function(readXML){
	
	var rss = readXML.getElementsByTagName('rss')[0];
	var channel = rss.getElementsByTagName('channel')[0];
 	var items = channel.getElementsByTagName('item');
    if( items.length ==0 ) { listDiv.innerHTML ='검색결과가 없습니다.'; return false; }
	
	var item_len = 0;


	var googleList= function(item_len) {
		for(var i=item_len; i< item_len+pagePerItem && i< items.length; i++) {
			
			var itemDiv= document.createElement('div');
            listDiv.appendChild(itemDiv);
            var descDiv = document.createElement('div');
            var aDiv = document.createElement('div');
            var descDiv2 = document.createElement('div');

			itemDiv.appendChild(aDiv);
            itemDiv.appendChild(descDiv);
			itemDiv.appendChild(descDiv2);
	
			itemDiv.className = "item";
            aDiv.className = "thumb";
            descDiv.className = "desc tit mar100";
			descDiv2.className = "desc tag mar100";
			
			var item= items.item(i);	
			var browser = navigator.appName;
			if( browser =='Microsoft Internet Explorer') {	var content = 'media:content'; var thumbnail = 'media:thumbnail'; }
			else {	var content = 'content'; var thumbnail = 'thumbnail'; }
			var title = item.getElementsByTagName('title')[0].firstChild.nodeValue;
			
			aDiv.innerHTML += '<a href='+item.getElementsByTagName(content)[0].getAttribute('url')+' title="'+title+'">'+
				'<img class=tn src='+item.getElementsByTagName(thumbnail)[0].getAttribute('url')+'></a>';
			aDiv.firstChild.onclick = function(e) { playerEvent(e); return false; }.bindAsEventListener(this);
			
			descDiv.innerHTML += '<a href='+item.getElementsByTagName(content)[0].getAttribute('url')+' title="'+title+'">'+title+'</a><br>';
			descDiv.firstChild.onclick= function(e){ playerEvent(e); return false;}.bindAsEventListener(this);
			var author = item.getElementsByTagName('author')[0].firstChild;
			if(author) descDiv2.innerHTML += '<br>'+ author.nodeValue+'<br>';

							
		}//for
	
		var allsee = document.createElement('span');
		pageDiv.appendChild(allsee);
	    var allseeUrl = rss.getElementsByTagName('allsee')[0].getAttribute('url');
		allsee.innerHTML += '<a href="'+allseeUrl+'"><img class=lbut src=images/w-btn_viewall.gif></a>';
				
		turnPage(googleList,items.length, item_len);
	
	}; //googleList

	googleList(item_len);

};//googleParser
	


	},

	onResize: function()
	{
	}
	

});
